// 预加载
$(document).ready(function() {
    layui.use('form', function() {
        var form = layui.form // 表单
        // 加载
        getUserInfo(form,user_id);
        // 监听事件
        // 训练场名称
        form.on("select(xlc001)", function(data) {
          console.log(data.value);
        })
        // 训练场地址
        form.on("select(xlc002)", function(data) {
            console.log(data.value);
        })
        // 教练名称
        form.on("select(staffNumber)", function(data) {
            console.log(data.value);
        })
        // 学员状态
        form.on("select(xy001)", function(data) {
            console.log(data.value);
        })
        // 驾考状态
        form.on("select(jk001)", function(data) {
            console.log(data.value);
        })
    });
})
// 接受传参
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
var user_id = getQueryString("user_id");
// 查询学员信息
function getUserInfo(form,id) {
    $.ajax({
        type: "get",
        url: "/user/getByIdUser",
        data: {
            "user_id": id
        },
        success: function (res) {
            console.log(res);
            var data = res.data;
            var xlc001v = '<option value="' + data.userCode.xlc001 + '" selected="selected">'+ data.userValue.xlc001 +'</option>';
            var xlc002v = '<option value="' + data.userCode.xlc002 + '" selected="selected">'+ data.userValue.xlc002 +'</option>';
            var staffNumberv = '<option value="' + data.userCode.staffNumber + '" selected="selected">'+ res.data.userValue.staffNumber +'</option>';
            var xy001v = '<option value="' + data.userCode.xy001 + '" selected="selected">'+ data.userValue.xy001 +'</option>';
            var jk001v = '<option value="' + data.userCode.jk001 + '" selected="selected">'+ data.userValue.jk001 +'</option>';

            $('#userName').val(data.userValue.userName);

            for (i=0;i<data.xlc001.length;i++) {
                xlc001v += "<option value='"+data.xlc001[i].code+"'>"+data.xlc001[i].showvalue+"</option>";
            }
            for (i=0;i<data.xlc002.length;i++) {
                xlc002v += "<option value='"+data.xlc002[i].code+"'>"+data.xlc002[i].showvalue+"</option>";
            }
            for (i=0;i<data.staff.length;i++) {
                staffNumberv += "<option value='"+data.staff[i].staNumber+"'>"+data.staff[i].staName+"</option>";
            }
            for (i=0;i<data.xy001.length;i++) {
                xy001v += "<option value='"+data.xy001[i].code+"'>"+data.xy001[i].showvalue+"</option>";
            }
            for (i=0;i<data.jk001.length;i++) {
                jk001v += "<option value='"+data.jk001[i].code+"'>"+data.jk001[i].showvalue+"</option>";
            }

            // 插入选择
            $("#xlc001").html("");
            $("#xlc001").append(xlc001v);

            $("#xlc002").html("");
            $("#xlc002").append(xlc002v);

            $("#staffNumber").html("");
            $("#staffNumber").append(staffNumberv);

            $("#xy001").html("");
            $("#xy001").append(xy001v);

            $("#jk001").html("");
            $("#jk001").append(jk001v);

            form.render('select');
        }
    })
}
// 更新值
function upUserInfo() {
    // var arrClass = new Array();
    // 添加值
    // arrClass.push("user_id",user_id);
    // arrClass.push("xlc001",$('#xlc001').val());
    // arrClass.push("xlc002",$('#xlc002').val());
    // arrClass.push("staffNumber",$('#staffNumber').val());
    // arrClass.push("xy001",$('#xy001').val());
    // arrClass.push("jk001",$('#jk001').val());

    $.ajax({
        type: 'post',
        url: '/user/updateByIdUser',
        dataType: "json",
        traditional: true,
        contentType : 'application/json;charset=utf-8',
        data:
            JSON.stringify({
                user_id: user_id,
                xlc001: $('#xlc001').val(),
                xlc002: $('#xlc002').val(),
                staffNumber: $('#staffNumber').val(),
                xy001: $('#xy001').val(),
                jk001:$('#jk001').val()
            }),
        success: function(res) {
            console.log('更新成功');
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.msg('更新成功');
            });
        }
    })
}